package com.bazaaroid.server.gae.core.dto.partner;

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

import com.bazaaroid.server.gae.core.dao.partner.ProductDAO;
import com.bazaaroid.server.gae.core.ioc.GuiceFactory;
import com.bazaaroid.server.gae.core.persistence.model.partner.Discount;
import com.bazaaroid.server.gae.core.persistence.model.partner.Product;

@XmlRootElement(name = "ActiveDiscounts")
public class ActiveDiscountsDTO {
	private List<Discount> discounts = null;

	public ActiveDiscountsDTO() {}

	public ActiveDiscountsDTO(List<Discount> discounts) {
		this.discounts = discounts;
	}

	@XmlRootElement(name = "Discount")
	public static class ActiveDiscountDTO {
		private Discount discount = null;

		public ActiveDiscountDTO() {}

		public ActiveDiscountDTO(Discount discount) {
			this.discount = discount;
		}

		@XmlElement
		public String getKey() {
			return discount.getKey().toString();
		}

		@XmlElement
		public int getPercentage() {
			return discount.getPercentage();
		}

		@XmlElement
		public Double getPriceWithDiscount() {
			return discount.getPriceWithDiscount();
		}

		@XmlElement
		public ProductSimpleDTO getProduct() {
			if(discount.getProduct() != null) {
				ProductDAO productDAO = GuiceFactory.getInjector()
					.getInstance(ProductDAO.class);
				Product product = productDAO.getById(discount.getProduct());
				return new ProductSimpleDTO(product);
			}

			return null;
		}

	}

	@XmlElement(name = "discount")
	@XmlElementWrapper(name = "discounts")
	public List<ActiveDiscountDTO> getActiveDiscounts() {
		List<ActiveDiscountDTO> activeDiscountsDTO = new ArrayList<ActiveDiscountDTO>();
		for(Discount discount: discounts)
			activeDiscountsDTO.add(new ActiveDiscountDTO(discount));

		return activeDiscountsDTO;
	}
}
